package org.zhadoop.stream.cql.semanticanalyzer.parser;

import org.antlr.v4.runtime.CommonTokenStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zhadoop.stream.cql.exception.ParseException;
import org.zhadoop.stream.cql.semanticanalyzer.parser.CQLParser.StatementContext;
import org.zhadoop.stream.cql.semanticanalyzer.parser.context.ParseContext;
import org.zhadoop.stream.cql.semanticanalyzer.parser.visitor.ApplicationVisitor;

/**
 * 解析完整的CQL语句
 * @author zwd
 *
 */
public class ApplicationParser implements IParser {

	private static final Logger LOG = LoggerFactory.getLogger(ApplicationParser.class);
	
	@Override
	public ParseContext parse(String cql) throws ParseException {
		LOG.info("start to parse cql: {}", cql);
		
		CQLLexer lexer = new CQLLexer(new ANTLRIgnoreCaseStringStream(cql));
		CommonTokenStream tokens = new CommonTokenStream(lexer);
		CQLParser parser = new CQLParser(tokens);
		StatementContext tree = parser.statement();
		ApplicationVisitor visitor = new ApplicationVisitor();
		return visitor.visit(tree);
	}

}
